home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 23 / AACD 23.iso / AACD / Online / FlushHeaders / old-source / OldSource.lha / FlushHeaders.c < prev    next >
C/C++ Source or Header  |  1999-05-14  |  9KB  |  402 lines

  1. /*
  2. ** $Log$
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9.  
  10. #include <proto/dos.h>
  11. #include <proto/exec.h>
  12. #include <proto/rexxsyslib.h>
  13.  
  14. #include <rexx/rxslib.h>
  15. #include <rexx/rexxio.h>
  16. #include <rexx/errors.h>
  17.  
  18. #include <dos/dos.h>
  19. #include <exec/memory.h>
  20. #include <utility/tagitem.h>
  21.  
  22. #include "get_folder_path.h"
  23.  
  24. int __stack = 12000;
  25.  
  26. struct NewsNode
  27. {
  28.     struct NewsNode *next;
  29.     struct NewsNode *prev;
  30.     struct List *domains;
  31.     char *ngname;
  32.     char *path;
  33.     long last;
  34.     long maxlen;
  35.     long maxmsg;
  36.     char folder[40];
  37.     char update;
  38. };
  39.  
  40. #ifndef __GNUC__
  41. struct Library        *RexxSysBase = NULL;
  42. #else
  43. struct RxsLib         *RexxSysBase = NULL;
  44. #endif
  45.  
  46. struct List *newsgroupslist=0;
  47. struct List   *folders;
  48.  
  49. void        *listpool=0;
  50.  
  51. char buf[1000];
  52.  
  53. char *stristr(char *buf, char *str)
  54. {
  55.     register int len = strlen(buf);
  56.     register int len2 = strlen(str);
  57.     register int a;
  58.  
  59.     for(a = 0; a <= len - len2; a++)
  60.         if(strnicmp(&buf[a], str, len2) == 0)
  61.             return(&buf[a]);
  62.     return(0);
  63. }
  64.  
  65. int LoadConfig(void)
  66. {
  67.     BPTR fh;
  68.     char *tmp;
  69.  
  70.     fh=Open("YAM:Yam2NN.config", MODE_OLDFILE);
  71.  
  72.     while(FGets(fh, buf, 200))
  73.     {
  74.         if(strlen(buf) < 2)
  75.             break;
  76.  
  77.         if(buf[0] == ';')
  78.             continue;
  79.  
  80.         if(!(strnicmp(buf, "Newsgroups:", 11)))
  81.         {
  82.             struct NewsNode *newsnode;
  83.  
  84.             if(!newsgroupslist)
  85.             {
  86.                 newsgroupslist = LibAllocPooled(listpool, sizeof(struct List));
  87.                 NewList(newsgroupslist);
  88.             }
  89.  
  90.             newsnode = LibAllocPooled(listpool, sizeof(struct NewsNode));
  91.  
  92.             newsnode->domains = LibAllocPooled(listpool, sizeof(struct List));
  93.             NewList(newsnode->domains);
  94.             newsnode->update = FALSE;
  95.  
  96.             tmp=strtok(buf + 12, " ");
  97.             newsnode->ngname = LibAllocPooled(listpool, strlen(tmp) + 1);
  98.             strcpy(newsnode->ngname, tmp);
  99.  
  100.             tmp=strtok(NULL, " \n");
  101.             if(!tmp)
  102.                 return(FALSE);
  103.  
  104.             newsnode->path = LibAllocPooled(listpool, strlen(tmp)+  1);
  105.             strcpy(newsnode->path, tmp);
  106.  
  107.             {
  108.                 int a, b;
  109.  
  110.                 a = strlen(tmp);
  111.                 if(tmp[a - 1] == '/')
  112.                     tmp[a - 1] = 0;
  113.                 while(tmp[a] != '/' && tmp[a] != ':' && a > 0)
  114.                     a--;
  115.  
  116.                 strcpy(newsnode->folder, &tmp[a + 1]);
  117.             }
  118.  
  119.             FGets(fh, buf, 200);
  120.             newsnode->maxlen = atol(&buf[13]);
  121.  
  122.             FGets(fh, buf, 200);
  123.             newsnode->maxmsg = atol(&buf[15]);
  124.  
  125.             while(1)
  126.             {
  127.                 struct Node *node;
  128.  
  129.                 if(!(FGets(fh, buf, 200)))
  130.                     break;
  131.  
  132.                 if(strnicmp(buf, "\n", 1)==0)
  133.                     break;
  134.             }
  135.  
  136.             AddTail(newsgroupslist, newsnode);
  137.         }
  138.     }
  139.     Close(fh);
  140.     return(TRUE);
  141. }
  142.  
  143. // this function was written by Christian Hattemer <Chris@heaven.riednet.wh.tu-darmstadt.de>
  144.  
  145. long SendRexxCommand(char *Port, char *Cmd, struct MsgPort *ReplyPort, char *Result)
  146. {
  147.    ULONG Error;
  148.    struct MsgPort *RexxPort;
  149.  
  150.    Forbid();
  151.  
  152.    if (RexxPort = FindPort(Port))
  153.    {
  154.       struct RexxMsg *rexxMsg, *Answer;
  155.  
  156.       if (rexxMsg = CreateRexxMsg(ReplyPort, NULL, NULL))
  157.       {
  158.          if (rexxMsg->rm_Args[0] = CreateArgstring(Cmd, strlen(Cmd)))
  159.          {
  160.             rexxMsg->rm_Action = RXCOMM | RXFF_RESULT;
  161.  
  162.             PutMsg(RexxPort, &rexxMsg->rm_Node);
  163.  
  164.             do
  165.             {
  166.                WaitPort(ReplyPort);
  167.                Answer = (struct RexxMsg *)GetMsg(ReplyPort);
  168.             } while (Answer == NULL);
  169.  
  170.             Permit();
  171.  
  172.             if ((Error = Answer->rm_Result1) == RC_OK)
  173.             {
  174.                if (Answer->rm_Result2)
  175.                {
  176.                   strcpy(Result, (STRPTR)Answer->rm_Result2);
  177.                   DeleteArgstring((UBYTE *)Answer->rm_Result2);
  178.                }
  179.             }
  180.  
  181.             DeleteArgstring((UBYTE *)ARG0(Answer));
  182.             DeleteRexxMsg(Answer);
  183.  
  184.             return Error;
  185.          }
  186.          else
  187.             DeleteRexxMsg(rexxMsg);
  188.       }
  189.    }
  190.  
  191.    Permit();
  192.  
  193.    return RC_FATAL;
  194. }
  195.  
  196. int FindOldHeader(char *in, char *out, char *header)
  197. {
  198.     int n = 0, i = 0;
  199.     char *h;
  200.     BOOL end=FALSE;
  201.  
  202.     h = strstr(in, header);
  203.  
  204.     if(h)
  205.     {
  206.         while(h[n] != ':')
  207.             n++;
  208.  
  209.         n += 2;
  210.  
  211.         while(!end)
  212.         {
  213.             while(h[n] != 10)
  214.                 out[i++] = h[n++];
  215.  
  216.             if(h[n-1] != ',')
  217.                 end = TRUE;
  218.             else
  219.             {
  220.                 while(isspace(h[n++])) ;
  221.                 n--;
  222.             }
  223.         }
  224.  
  225.         out[i]=0;
  226.         return(TRUE);
  227.     }
  228.     return(FALSE);
  229. }
  230.  
  231. void GetFolderName(char *folder, char *path)
  232. {
  233.     char fconfig[108];
  234.     BPTR fh;
  235.  
  236.     strcpy(fconfig, path);
  237.     AddPart(fconfig, ".fconfig", 108);
  238.     fh=Open(fconfig, MODE_OLDFILE);
  239.  
  240.     while(FGets(fh, fconfig, 108))
  241.     {
  242.         if(strnicmp("name", fconfig, 4)==0)
  243.         {
  244.             int a=0, b=0;
  245.  
  246.             while(fconfig[a++]!='=') ;
  247.  
  248.             a++;
  249.             while(isspace(fconfig[a++])) ;
  250.  
  251.             a--;
  252.             while(fconfig[a]!=10)
  253.                 folder[b++]=fconfig[a++];
  254.  
  255.             folder[b]=0;
  256.             break;
  257.         }
  258.     }
  259.     Close(fh);
  260. }
  261.  
  262. void update_folder(void)
  263. {
  264.     struct MsgPort *ARexxPort;
  265.     char tmp[100];
  266.  
  267.     ARexxPort = CreateMsgPort();
  268.     if(!ARexxPort)
  269.         return;
  270.  
  271.     sprintf(buf, "SETFOLDER %d", get_folder_pos(folders, DELETED));
  272.     SendRexxCommand("YAM", buf, ARexxPort, tmp);
  273.  
  274.     SendRexxCommand("YAM", "MAILUPDATE", ARexxPort, tmp);
  275.  
  276.     sprintf(buf, "SETFOLDER %d", ((struct Folder *)folders->lh_Head)->f_num);
  277.     SendRexxCommand("YAM", buf, ARexxPort, tmp);
  278.  
  279.  
  280.     DeleteMsgPort(ARexxPort);
  281. }
  282.  
  283. int main(int argc, char *argv[])
  284. {
  285.     struct FileInfoBlock *fib;
  286.     BPTR lock, fh, oldlock;
  287.     char *msg = 0, tmp[108], path[108];
  288.  
  289.     RexxSysBase = OpenLibrary("rexxsyslib.library", 0);
  290.     if(!RexxSysBase)
  291.         return(20);
  292.  
  293.     query_for_user();
  294.  
  295.     folders = init_folder_list();
  296.  
  297.     strcpy(path, current_user_path);
  298.     strcpy(tmp, get_folder_path(folders, DELETED));
  299.  
  300.     AddPart(path, tmp, sizeof(path));
  301.  
  302.     strcpy(buf, path);
  303.     AddPart(buf, ".fconfig", sizeof(buf));
  304.  
  305.     lock = Lock(buf, ACCESS_READ);
  306.     if(!lock)
  307.         exit(20);
  308.  
  309.     UnLock(lock);
  310.  
  311.     if(!(listpool = LibCreatePool(MEMF_ANY | MEMF_CLEAR, 4096, 4096)))
  312.         exit(20);
  313.  
  314.     fib = AllocDosObject(DOS_FIB, TAG_DONE);
  315.  
  316.     if(!(LoadConfig()))
  317.         exit(20);
  318.  
  319.     lock = Lock(path, ACCESS_READ);
  320.     if(!lock)
  321.         exit(20);
  322.     oldlock = CurrentDir(lock);
  323.  
  324.     Examine(lock, fib);
  325.  
  326.     while(ExNext(lock, fib))
  327.     {
  328.         if(fib->fib_EntryType < -2)
  329.         {
  330.             if(stricmp(fib->fib_FileName, ".fconfig") && stricmp(fib->fib_FileName, ".index"))
  331.             {
  332.                 char header[60];
  333.  
  334.                 fh = Open(fib->fib_FileName, MODE_OLDFILE);
  335.                 msg = AllocVec(fib->fib_Size, MEMF_ANY | MEMF_CLEAR);
  336.                 Read(fh, msg, fib->fib_Size);
  337.  
  338.                 if(FindOldHeader(msg, header, "Message-ID:"))
  339.                 {
  340.                     if(FindOldHeader(msg, tmp, "Newsgroups:"))
  341.                     {
  342.                         BPTR fh2;
  343.                         char name[108], *p, *nextname;
  344.                         struct NewsNode *nn = (struct NewsNode *)newsgroupslist->lh_Head;
  345.  
  346.                         nextname = strtok(tmp, ",");
  347.                         while(nextname)
  348.                         {
  349.                             char folder[108];
  350.  
  351.                             strcpy(name, "YAM:NNTP-Headers/");
  352.  
  353.                             while(nn->next)
  354.                             {
  355.                                 if(stricmp(nn->ngname, nextname) == 0)
  356.                                     break;
  357.                                 nn = nn->next;
  358.                             }
  359.  
  360.                             GetFolderName(folder, nn->path);
  361.                             strcat(name, folder);
  362.  
  363.                             header[0] = 'p';
  364.                             if(strlen(header) <= 30)
  365.                             {
  366.                                 p = strchr(header, '>');
  367.                                 if(p)
  368.                                     *p = 0;
  369.                             }
  370.                             else
  371.                                 header[30] = 0;
  372.  
  373.                             AddPart(name, header, sizeof(name));
  374.                             fh2 = Open(name, MODE_OLDFILE);
  375.                             if(fh2)
  376.                             {
  377.                                 Close(fh2);
  378.                                 DeleteFile(name);
  379.                             }
  380.                             nextname = strtok(NULL, ",");
  381.                             if(!nextname)
  382.                                 break;
  383.                         }
  384.                     }
  385.                 }
  386.                 FreeVec(msg);
  387.                 Close(fh);
  388.                 DeleteFile(fib->fib_FileName);
  389.             }
  390.         }
  391.     }
  392.  
  393.     FreeDosObject(DOS_FIB, fib);
  394.     CurrentDir(oldlock);
  395.     UnLock(lock);
  396.     LibDeletePool(listpool);
  397.     update_folder();
  398.     free_folders_list(folders);
  399.     CloseLibrary(RexxSysBase);
  400.     exit(0);
  401. }
  402.